import Vue from "vue";
import VueRouter from "vue-router";
import Home from "@/view/home/home";
import Personal from "@/view/personalCenter/personal/personal";
Vue.use(VueRouter);

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
const routes = [
  {
    path: "/",
    redirect: "/index",
  },
  {
    path: "/login",
    name: "login",
    meta: {
      title: "登录",
    },
    component: () => import("@/view/login/login"),
  },
  {
    path: "/register",
    name: "register",
    meta: {
      title: "注册",
    },
    component: () => import("@/view/register/register"),
  },
  {
    path: "/index",
    name: "home",
    component: Home,
    meta: {
      title: "首页",
    },
    children: [
      {
        path: "/index",
        name: "index",
        meta: {
          title: "首页",
        },
        component: () => import("@/view/index/index"),
      },
      {
        path: "/privacy",
        name: "privacy",
        meta: {
          title: "隐私政策",
        },
        component: () => import("@/view/agreement/privacy"),
      },
      {
        path: "/service",
        name: "service",
        meta: {
          title: "用户服务协议",
        },
        component: () => import("@/view/agreement/service"),
      },
      {
        path: "/ApplicationProducts",
        name: "ApplicationProducts",
        meta: {
          title: "应用产品",
        },
        component: () =>
          import("@/view/ApplicationProducts/ApplicationProducts"),
      },
      {
        path: "/ApplicationProductsDetail",
        name: "ApplicationProductsDetail",
        meta: {
          title: "应用产品详情",
        },
        component: () =>
          import("@/view/ApplicationProducts/ApplicationProductsDetail"),
      },
			{
			  path: "/ExchangeResults",
			  name: "ExchangeResults",
			  meta: {
			    title: "成果交流",
			  },
			  component: () =>
			    import("@/view/ExchangeResults/ExchangeResults"),
			},
			{
			  path: "/ExchangeResultsDetail",
			  name: "ExchangeResultsDetail",
			  meta: {
			    title: "成果交流详情",
			  },
			  component: () =>
			    import("@/view/ExchangeResults/ExchangeResultsDetail"),
			},
			{
			  path: "/AlgorithmTools",
			  name: "AlgorithmTools",
			  meta: {
			    title: "算法工具",
			  },
			  component: () =>
			    import("@/view/AlgorithmTools/AlgorithmTools"),
			},
			{
			  path: "/AlgorithmToolsDetail",
			  name: "AlgorithmToolsDetail",
			  meta: {
			    title: "算法工具详情",
			  },
			  component: () =>
			    import("@/view/AlgorithmTools/AlgorithmToolsDetail"),
      },
      {
        path: "/industryNewDetail",
        name: "industryNewDetail",
        meta: {
          title: "行业资讯详情",
        },
        component: () => import("@/view/ApplicationProducts/industryNewDetail"),
      },
      {
        path: "/planPromotion",
        name: "planPromotion",
        meta: {
          title: "方案推广",
        },
        component: () => import("@/view/planPromotion/planPromotion"),
      },
      {
        path: "/planPromotionDetail",
        name: "planPromotionDetail",
        meta: {
          title: "方案推广详情",
        },
        component: () => import("@/view/planPromotion/planPromotionDetail"),
      },
      {
        path: "/planCollectDetail",
        name: "planCollectDetail",
        meta: {
          title: "方案征集详情",
        },
        component: () => import("@/view/planPromotion/planCollectDetail"),
      },
      {
        path: "/information",
        name: "information",
        meta: {
          title: "最新资讯",
        },
        component: () => import("@/view/information/information"),
      },
      {
        path: "/informationDetail",
        name: "informationDetail",
        meta: {
          title: "资源共享详情",
        },
        component: () => import("@/view/information/informationDetail"),
      },
      {
        path: "/topicDetail",
        name: "topicDetail",
        meta: {
          title: "创新课题详情",
        },
        component: () => import("@/view/innovationConsortium/topicDetail"),
      },
      {
        path: "/topicCollectDetail",
        name: "topicCollectDetail",
        meta: {
          title: "课题组征集详情",
        },
        component: () =>
          import("@/view/innovationConsortium/topicCollectDetail"),
      },
      {
        path: "/innovationConsortium",
        name: "innovationConsortium",
        meta: {
          title: "创新联合体",
        },
        component: () =>
          import("@/view/innovationConsortium/innovationConsortium"),
      },
      {
        path: "/forgetPwdEmail",
        name: "forgetPwdEmail",
        meta: {
          title: "忘记密码-邮箱用户找回密码",
        },
        component: () => import("@/view/forgetPwd/forgetPwdEmail"),
      },
      {
        path: "/studyGarden",
        name: "studyGarden",
        meta: {
          title: "学习园地",
        },
        component: () => import("@/view/studyGarden/studyGarden"),
      },
      {
        path: "/demandCollect",
        name: "demandCollect",
        meta: {
          title: "需求征集",
        },
        component: () => import("@/view/demandCollect/demandCollect"),
      },
      {
        path: "/shopCart",
        name: "shopCart",
        meta: {
          title: "购物车",
          requireAuth: true,
        },
        component: () => import("@/view/personalCenter/shopCart/shopCart"),
      },
      {
        path: "/applyShare",
        name: "applyShare",
        meta: {
          title: "申请共享页",
          requireAuth: true,
        },
        component: () => import("@/view/personalCenter/shopCart/applyShare"),
      },
      {
        path: "/applyResult",
        name: "applyResult",
        meta: {
          title: "产品申请成功页",
          requireAuth: true,
        },
        component: () => import("@/view/personalCenter/shopCart/applyResult"),
      },
      {
        path: "/studyGarden",
        name: "studyGarden",
        meta: {
          title: "学习园地",
        },
        component: () => import("@/view/studyGarden/studyGarden"),
      },
      {
        path: "/studyGardenDetail",
        name: "studyGardenDetail",
        meta: {
          title: "学习园地详情",
        },
        component: () => import("@/view/studyGarden/studyGardenDetail"),
      },
      {
        path: "/demandCollect",
        name: "demandCollect",
        meta: {
          title: "需求征集",
        },
        component: () => import("@/view/demandCollect/demandCollect"),
      },
      {
        path: "/demandCollectDetail",
        name: "demandCollectDetail",
        meta: {
          title: "需求详情",
        },
        component: () => import("@/view/demandCollect/demandCollectDetail"),
      },
      {
        path: "/specialProduct",
        name: "specialProduct",
        meta: {
          title: "专题推荐",
        },
        component: () => import("@/view/specialProduct/specialProduct"),
      },
	  {
	    path: "/search",
	    name: "search",
	    meta: {
	      title: "搜索列表",
	    },
	    component: () => import("@/view/search/search"),
	  },
      {
        path: "/personal",
        name: "personal",
        meta: {
          title: "个人中心",
        },
        component: Personal,
        // eslint-disable-next-line no-sparse-arrays
        children: [
          {
            path: "/basicInfo",
            name: "basicInfo",
            meta: {
              title: "基本信息",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/basicInfo/basicInfo"),
          },

          {
            path: "/changeInfo",
            name: "changeInfo",
            meta: {
              title: "用户信息修改",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/basicInfo/changeInfo"),
          },
          {
            path: "/changePwd",
            name: "changePwd",
            meta: {
              title: "修改密码",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/basicInfo/changePwd"),
          },
          {
            path: "/invitationCode",
            name: "invitationCode",
            meta: {
              title: "企业邀请码",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/basicInfo/invitationCode"),
          },
          {
            path: "/changeInfoPhone",
            name: "changeInfoPhone",
            meta: {
              title: "修改手机号",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/basicInfo/changeInfoPhone"),
          },
          {
            path: "/changeEmail",
            name: "changeEmail",
            meta: {
              title: "修改邮箱",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/basicInfo/changeEmail"),
          },
          {
            path: "/changeRealName",
            name: "changeRealName",
            meta: {
              title: "修改用户实名认证",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/basicInfo/changeRealName"),
          },
          {
            path: "/changeRegInfo",
            name: "changeRegInfo",
            meta: {
              title: "修改注册信息",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/basicInfo/changeRegInfo"),
          },
          {
            path: "/msgCenter",
            name: "msgCenter",
            meta: {
              title: "消息中心",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/msgCenter/msgCenter"),
          },
          {
            path: "/msgCenterNone",
            name: "msgCenter",
            meta: {
              title: "消息中心",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/msgCenter/msgCenter"),
          },

          {
            path: "/newManage",
            name: "newManage",
            meta: {
              title: "栏目管理",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/newManage/newManage"),
          },
          {
            path: "/newContent",
            name: "newContent",
            meta: {
              title: "资讯发布",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/newManage/newContent"),
          },
          // {
          //   path: "/industryNewMine",
          //   name: "industryNewMine",
          //   meta: {
          //     title: "我的资讯",
          //     requireAuth: true,
          //   },
          //   component: () =>
          //     import("@/view/personalCenter/industryNewMine/newContent"),
          // },
          {
            path: "/myIndustryNew",
            name: "myIndustryNew",
            meta: {
              title: "我的资讯",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/myIndustryNew/newContent"),
          },
          {
            path: "/myPlanPromotion",
            name: "myPlanPromotion",
            meta: {
              title: "我的方案",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/myPlanPromotion/newContent"),
          },
          {
            path: "/myTopic",
            name: "myTopic",
            meta: {
              title: "我的课题",
              requireAuth: true,
            },
            component: () => import("@/view/personalCenter/myTopic/newContent"),
          },
          {
            path: "/newExamine",
            name: "newExamine",
            meta: {
              title: "资讯发布审核",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/newManage/newExamine"),
          },
          {
            path: "/commentList",
            name: "commentList",
            meta: {
              title: "我的评论",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/myComment/commentList"),
          },
          {
            path: "/resourceShare",
            name: "resourceShare",
            meta: {
              title: "资源共享",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/resourceShare/resourceShare"),
          },
          {
            path: "/myApply",
            name: "myApply",
            meta: {
              title: "我的申请",
              requireAuth: true,
            },
            component: () => import("@/view/personalCenter/myApply/applyList"),
          },
          {
            path: "/federationList",
            name: "federationList",
            meta: {
              title: "产教联合体",
              requireAuth: true,
            },
            component: () =>
              import("@/view/personalCenter/eduFederation/federationList"),
          },
          {
            path: "/myDemand",
            name: "myDemand",
            meta: {
              title: "我的审核",
              // requireAuth: true,
            },
            component: () => import("@/view/personalCenter/demand/newContent"),
          },
          {
            path: "/myOrder",
            name: "myOrder",
            meta: {
              title: "我的订单",
              // requireAuth: true,
            },
            component: () => import("@/view/personalCenter/myOrder/myOrder"),
          },
        ],
      },
    ],
  },
  // {
  //     path: "*",
  //     component: () => import('@/view/error/404')
  // }
];
let router = new VueRouter({
  routes,
  mode: "history",
  // scrollBehavior
  scrollBehavior(to, from, savedPosition) {
    // to访问的页面    from当前的页面   savedPosition返回{x：0，y：0}  分别代表x轴和y轴的滚动条（scrollTop）
    if (savedPosition) {
      return savedPosition;
    } else {
      return { x: 0, y: 0 };
    }
  },
});
router.beforeEach((to, from, next) => {
  //beforeEach是router的钩子函数，在进入路由前执行
  document.title = to.meta.title + "-数据资产运营平台";
  // document.title = "园企综合服务平台";
  // document.documentElement.scrollTop = 0; // 让页面回到顶部
  // next();
  if (to.meta.requireAuth) {
    // var token = window.sessionStorage.getItem("access_token");
    var token = window.sessionStorage.getItem("userInfo");
    // 判断该路由是否需要登录权限
    if (token) {
      //获取当前的token是否存在
      next();
    } else {
      next({
        path: "/login",
        query: { redirect: to.fullPath }, // 将跳转的路由path作为参数，登录成功后跳转到该路由
      });
    }
  } else {
    next();
  }
});

export default router;
